package com.jandar.mvc.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@DynamicUpdate
@DynamicInsert
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty("主键")
    protected Integer id;

    // 前端要求接收但不处理该参数
    @Transient
    private String key;
    @Transient
    private Integer pageSize;

    @Transient
    private Integer pageNumber;
    @Column(updatable = false)
    @ApiModelProperty("创建时间")
    private LocalDateTime createTime;

    @Column
    @ApiModelProperty("更新时间")
    private LocalDateTime updateTime;

    @Column(name = "created_by", updatable = false, length = 64)
    @ApiModelProperty("创建用户id")
    private String createdBy;

    @Column(name = "updated_by", length = 64)
    @ApiModelProperty("最后一次更新用户id")
    private String updatedBy;

    @ApiModelProperty("创建用户名")
    @Column(name = "created_by_show_name", length = 64)
    private String createdByShowName;


    @ApiModelProperty("最后一次更新用户名")
    @Column(name = "updated_by_show_name", length = 64)
    private String updatedByShowName;

    @ApiModelProperty("查询权限")
    private String power;
    @Column(columnDefinition = "varchar(32) default ''")
    @ApiModelProperty("系统代码")
    private String systemCode;
    @Transient
    private Map<String, Object> params = new HashMap<>();

    @PrePersist
    protected void onCreate() {
        createTime = LocalDateTime.now();
        updateTime = LocalDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updateTime = LocalDateTime.now();
    }
}
